﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace WeiSiCopy 
{
    public class DBroot
    {
        public OleDbConnection Conn1;
        public bool ConnectionState = false;
        public string ExceptionMsg = "";
        public bool FindCaseData = false;

        public DBroot(string ConnStr)
        {
            Conn1 = new OleDbConnection();
            Conn1.ConnectionString = ConnStr;
            try
            {
                Conn1.Open();
                ConnectionState = true;
            }
            catch (Exception e)
            {
                ExceptionMsg = e.Message;
                ConnectionState = false;
            }
        }

        private void CloseConnect()
        {
            Conn1.Close();
        }

        private OleDbConnection GetOpenConnect()
        {
            if (Conn1.State != System.Data.ConnectionState.Open)
            {
                try
                {
                    Conn1.Open();
                    ConnectionState = true;
                    return Conn1;
                }
                catch (Exception e)
                {
                    ExceptionMsg = e.Message;
                    ConnectionState = false;
                }
            }
            return null;
        }

        protected void ExecSel(string strSQL)
        {
            OleDbTransaction ITran = null;

            if (Conn1.State != System.Data.ConnectionState.Open) Conn1.Open();

            try
            {

                ITran = Conn1.BeginTransaction();

                OleDbCommand myCommand = new OleDbCommand(strSQL.ToString());
                myCommand.Connection = Conn1;
                myCommand.Transaction = ITran;
                myCommand.ExecuteNonQuery();

                ITran.Commit();
            }
            catch (Exception e)
            {
                ExceptionMsg = e.Message;
                if (ITran != null)
                    ITran.Rollback();
            }
            finally
            {
                Conn1.Close();
            }
        }

        protected DataTable ExecSelTD(string strSQL)
        {
            DataTable TD = new DataTable();

            try
            {
                if (Conn1.State != System.Data.ConnectionState.Open) Conn1.Open();
                OleDbDataAdapter myAdapter = new OleDbDataAdapter(strSQL.ToString(), Conn1);
                myAdapter.Fill(TD);
            }
            catch
            {
            }

            return TD;
        }


    }

    public class Apply : DBroot
    {
        public string SrcSiName { get; set; }
        public string DesSiName { get; set; }
        public int status { get; set; }
        public string ApplyName { get; set; }

        public Apply(string ConnStr)
            :base(ConnStr)
        {
        }

        
        public bool IsExist()
        {
            bool flag = false;

            StringBuilder strSQL = new System.Text.StringBuilder("");
            strSQL.AppendLine("select * ");
            strSQL.AppendLine("from Apply ");
            strSQL.AppendLine("where PageName = '" + this.DesSiName + "' and status = 0  ");
           
            DataTable DT = ExecSelTD(strSQL.ToString());

            if (DT != null && DT.Rows.Count > 0)
            {
                this.ApplyName = DT.Rows[0]["ApplyName"].ToString();

                flag = true;
            }

            return flag;
        }

        public void Update()
        {
            bool flag = false;

            StringBuilder strSQL = new System.Text.StringBuilder("");
            strSQL.AppendLine("update  Apply set status = "+ this.status+" ");
            strSQL.AppendLine("where PageName = '" + this.DesSiName + "'   ");
            strSQL.AppendLine("insert into ApplyLog (ApplyID,LogType,LogStatus)values(");
            strSQL.AppendLine("(select applyid from Apply where PageName='" + this.DesSiName + "'),'2','" + this.status + "');");
            ExecSel(strSQL.ToString());

        }

    }
}
